<template>
	<view class="zai-box">

		<image src="https://xinguan-parent2.oss-cn-hangzhou.aliyuncs.com/zaizai-login/login.png" mode='aspectFit'
			class="zai-logo"></image>
		<view class="zai-title">登录</view>
		<view class="zai-form">
			<input class="zai-input" v-model="username" placeholder="请输入手机号" />
			<input class="zai-input" v-model="password" password placeholder="请输入密码" />
			<view class="zai-label">
				<view class="zai-label_left" @click="forget()">
					忘记密码？
				</view>
				<view class="zai-label_left" @click="mobil()">
					手机号登录
				</view>
			</view>
			<button class="zai-btn" @click="login()">立即登录</button>
			<navigator url="../register/register01" hover-class="none" class="zai-label">还没有账号？点此注册.</navigator>
		</view>
		
		<view class="login-way">
			<view class="way" @tap="auth">
				<u-icon name="weixin-fill" color="#2979ff" size="28"></u-icon>
				<text>微信登录</text>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				username: '', //手机号码
				password: '' ,//密码
				openid: '',
				// 表单
				form: {
					phone: '',
					code: '',
					password: '',
				},
				Cuser: {},
				flag:false
			};
		},
		computed: {
			...mapState(['islogin']),
		},
		
		methods: {
			forget() {
				uni.navigateTo({
					url: '../forgot/forgot'
				})
			},
			mobil() {
				uni.navigateTo({
					url: '../mobile/mobile'
				})
			},
			onLoad() {
				this.logins()
			},
			logins() {
				uni.login({
					provider: 'weixin',
					success: res => {
						console.log(res);
						this.$u.http.get('/app/login/'+this.appid+'/'+res.code, {
							appid: this.appid,
							code: res.code
						}).then(res => {
							console.log(res);
							this.openid = res.openid
							
							if(res.code==0)
							{
								this.flag = true
							}
						})
					}
				})
			},
			
			auth() {
				if(this.flag)
				{
					this.$u.http.get("/app/GetMapping/"+ this.openid).then(res => {
						console.log(res);
						uni.setStorageSync('token', res.token)
						this.$store.dispatch('set_userinfo', res.users)
						this.$store.dispatch('set_islogin', true)
					
						uni.showToast({
							title:'登录成功',
								type: 'success',
						})
						setTimeout(()=>{
							uni.switchTab({
								url: "/pages/bottom-nav/index/index"
							})
						}, 2000);
					})
					return
				}
				wx.getUserProfile({
					lang: 'zh_CN',
					desc: '注册',
					success: res => {
						this.Cuser = {
							nickname: res.userInfo.nickName,
							sex: res.userInfo.gender,
							avatar: res.userInfo.avatarUrl,
							city: res.userInfo.city,
						}
						this.Cuser.openid = this.openid,
							
						this.$u.http.post("/app/postAuth",this.Cuser).then(res => {
							console.log(res);
							uni.setStorageSync('token', res.token)
							this.$store.dispatch('set_userinfo', res.users)
							this.$store.dispatch('set_islogin', true)
							this.$refs.uToast.show({
								title: '登录成功',
								type: 'success',
							})
							setTimeout(function() {
								uni.switchTab({
									url: "/pages/bottom-nav/index/index"
								})
							}, 2000);
						})
			
					},
					fail: err => {
						console.log(err);
					}
				})
			},
			
			//当前登录按钮操作
			 async  login() {
				// console.log("bb");
				if (!this.username) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.username)) {
					uni.showToast({
						title: '请输入正确手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				// uni.$u.http.post('/app/login').then(
				// 	(res)=>
				// 	{
				// 		console.log(res);
				// 	}
				// )



			 await 	uni.$u.http.post('/app/login', {
					"mobile": this.username,
					"password": this.password
				}).then((res) => {
					
					if (res != null) {
						uni.setStorageSync('token', res.token)
						this.$store.dispatch('set_userinfo', res.users)
						this.$store.dispatch('set_islogin', true)

						uni.showToast({
							title: '登录成功',
							icon: 'none'
						});
						setTimeout(function() {
							uni.switchTab({
								url: "/pages/bottom-nav/index/index"
							})
						}, 2000);
					}


				}).catch(() => {

				})
			}
		}
	}
</script>

<style>
	.zai-box {
		padding: 0 100upx;
		position: relative;
	}

	.zai-logo {
		width: 100%;
		width: 100%;
		height: 310upx;
	}

	.zai-title {
		position: absolute;
		top: 0;
		line-height: 360upx;
		font-size: 68upx;
		color: #fff;
		text-align: center;
		width: 100%;
		margin-left: -100upx;
	}

	.zai-form {
		margin-top: 300upx;
	}

	.zai-input {
		background: #e2f5fc;
		margin-top: 30upx;
		border-radius: 100upx;
		padding: 20upx 40upx;
		font-size: 36upx;
	}

	.input-placeholder,
	.zai-input {
		color: #94afce;
	}

	.zai-label {
		padding: 60upx 0;
		text-align: center;
		font-size: 30upx;
		color: #a7b6d0;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
	}

	.zai-btn {
		background: #ff65a3;
		color: #fff;
		border: 0;
		border-radius: 100upx;
		font-size: 36upx;
	}

	.zai-btn:after {
		border: 0;
	}

	/*按钮点击效果*/
	.zai-btn.button-hover {
		transform: translate(1upx, 1upx);
	}
</style>
